import { Body, Controller, Get, Inject, Post } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { User } from '../entity/user';
import { Repository } from 'typeorm';
import { RedisService } from '@midwayjs/redis';
import { ApiResponse } from 'midwayjs-kinfe4j2';
import { MidwayI18nService } from '@midwayjs/i18n';
import { UserDto } from '../dto/userDto';

@Controller('/')
export class HomeController {
  // 自动注入类型
  @InjectEntityModel(User)
  userModel: Repository<User>;

  @Inject()
  redisService: RedisService;

  @Inject()
  i18nService: MidwayI18nService;

  @Get('/')
  @ApiResponse({ status: 200, description: '首页' })
  // async home(): Promise<User[]> {
  async home(): Promise<string> {
    // 测试redis
    // Redis--》设置值
    await this.redisService.set('name', '张三');
    // Redis--》获取值
    // return await this.redisService.get('name');

    // 测试国际化
    return this.i18nService.translate('hello', {
      locale: 'en_US',
    });

    // 测试查询user表中数据
    // return await this.userModel.find();
  }

  // 查询所有
  @Get('/find')
  @ApiResponse({ status: 200, description: '查询所有' })
  async findAll(): Promise<User[]> {
    // 测试查询user表中数据
    return await this.userModel.find();
  }

  @Post('/add')
  async homeSave(@Body() user: UserDto): Promise<void> {
    console.log(user);
  }
}
